﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Design Pattern</title>
    <meta name="language" content="en" />
    <!--myInjectedScript-->
</head>
<body>
    <h1>
        State</h1>
    <div class="section">
        <h2>
            Intent</h2>
        <p>
            Allow an object to alter its behavior when its internal state changes.The object
            will appear to change its class.<br />
            
        </p>
    </div>
    <div class="section">
        <h2>
            Also known as</h2>
        <p>
            Objects for States</p>
    </div>
    <div class="section">
        <h2>
            Motivation</h2>
        <p>
            The State pattern allows an object to behave differently depending on its internal
            state. This state is usually represented by one or more enumerated constants.<br />
            The difference in behavior is delegated to objects that represent this state.
            <br /><a href="http://en.wikipedia.org/wiki/State_pattern" target="_blank">Online resources</a>
        </p>
    </div>
    <div class="section">
        <h2>
            Applicability</h2>
        <p>
            <ol>
                <li>An object's behavior depends on its state, and it must change its behavior at run-time
                    depending on that state.</li>
                <li>Operations have large, multipart conditional statements that depend on the object's
                    state.<br />
                    This state is usually represented by one or more enumerated constants. Often, several
                    operations will contain this same conditional structure. The State pattern puts
                    each branch of the conditional in a separate class. This lets you treat the object's
                    state as an object in its own right that can vary independently from other objects.
                </li>
            </ol>
        </p>
    </div>
    <div class="section">
        <h2>
            Class Diagram</h2>
        <p>
            <img alt="Diagram" src=" state.png" border="0" />
        </p>
    </div>
    <div class="section">
        <h2>
            Collaborations</h2>
        <p>
            <ol>
                <li>Context delegates state-specific requests to the current ConcreteState object</li>
                <li>A context may pass itself as an argument to the State object handling the request.
                    This lets the State object access the context if necessary. </li>
                <li>Context is the primary interface for clients. Clients can configure acontext with
                    State objects. Once a context is configured, its clients don't have to deal with
                    the State objects directly.</li>
                <li>Either Context or the ConcreteState subclasses can decide which state succeeds another
                    and under what circumstances. </li>
            </ol>
        </p>
    </div>
    <div class="section">
        <h2>
            Consequences</h2>
        <p>
            <ol>
                <li>It localizes state-specific behavior and partitions behavior for different states.The
                    State pattern puts all behavior associated with a particular state in to one object.
                    Because all state-specific code lives in a State subclass, new states and transitions
                    can be added easily by defining new subclasses. </li>
                <li>It makes state transitions explicit.When an object defines its current state solely
                    in terms of internal data values,its state transitions have no explicit representation;they
                    only show up as assignments to some variables. Introducing separateobjects for different
                    states makes the transitions moreexplicit. Also, State objects can protect the Context
                    from inconsistent internal states.</li>
                <li>State objects can be shared.If State objects have no instance variables—that is,
                    the state they represent is encoded entirely in their type,then contexts can share
                    a State object.</li>
            </ol>
        </p>
    </div>
    <div class="section">
        <h2>
            Related Patterns</h2>
        <p>
            <ul>
                <li>The <a href="">Flyweight</a> pattern explains when and how State objects can be shared.</li>
                <li><a href="">State</a> objects are often <a href="">Singletons</a></li>
            </ul>
        </p>
    </div>
</body>
</html>
